package com.bw.wxy.mvp.view;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.bw.wxy.R;
import com.bw.wxy.fragment.Fragment_account;
import com.bw.wxy.fragment.Fragment_register;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout fragmentVesselParent;
    private LinearLayout fragmentVessel;
    private Fragment_account fragment_account;
    private Fragment_register fragment_register;
    private TextView loginSwitch;
    private ImageView loginBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        fragment_account = new Fragment_account();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragment_vessel, fragment_account).commit();

    }

    private void initView() {
        fragmentVesselParent = findViewById(R.id.fragment_vessel_parent);
        fragmentVessel = findViewById(R.id.fragment_vessel);
        loginSwitch = findViewById(R.id.login_switch);
        loginSwitch.setOnClickListener(this::onClick);
        loginBack = findViewById(R.id.login_back);
        loginBack.setOnClickListener(this::onClick);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login_switch:
                FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                AnimatorSet animatorSet = new AnimatorSet();
                ObjectAnimator translationY = ObjectAnimator.ofFloat(fragmentVesselParent, "translationY", 1, 300);
                ObjectAnimator alpha = ObjectAnimator.ofFloat(fragmentVesselParent, "alpha", 1,0);
                ObjectAnimator translationY2 = ObjectAnimator.ofFloat(fragmentVesselParent, "translationY", 300, 1);
                ObjectAnimator alpha2 = ObjectAnimator.ofFloat(fragmentVesselParent, "alpha", 0, 1);
                animatorSet.setDuration(800);
                animatorSet.playTogether(translationY,alpha,translationY2,alpha2);
                animatorSet.start();
                if (loginSwitch.getText().toString().trim().equals("手机登录")) {
                    loginSwitch.setText("账号登录");
                    editImg(R.mipmap.res_7icon_364);
                    if (fragment_register == null) {
                        fragment_register = new Fragment_register();
                        fragmentTransaction.add(R.id.fragment_vessel, fragment_register);
                    }
                    fragmentTransaction.hide(fragment_account);
                    fragmentTransaction.show(fragment_register);
                } else {
                    loginSwitch.setText("手机登录");
                    editImg(R.mipmap.res_7icon_363);
                    fragmentTransaction.hide(fragment_register);
                    fragmentTransaction.show(fragment_account);
                }
                fragmentTransaction.commit();
                break;
            case R.id.login_back:
                finish();
                break;
        }


    }

    private void editImg(int p) {
        Drawable leftDrawable = null;
        leftDrawable = getResources().getDrawable(p);
        leftDrawable.setBounds(0, 0, leftDrawable.getMinimumWidth(), leftDrawable.getMinimumHeight());
        loginSwitch.setCompoundDrawables(leftDrawable, null, null, null);
    }
}